package com.lfp.config.rabbitmq;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description 发布确认模式 配置类
 *
 *  交换机、队列关系图参考：./images/confirm.png
 *
 * @Author lfp
 * @Date 2022/7/19
 * @Version 1.0
 */
@Configuration
public class ConfirmConfig {

    //交换机名称
    public static final String CONFIRM_EXCHANGE_NAME = "confirm.exchange";
    //队列名称
    public static final String CONFIRM_QUEUE_NAME = "confirm.queue";
    //路由键
    public static final String CONFIRM_ROUTING_KEY = "key1";


    //声明交换机
    @Bean
    public DirectExchange confirmExchange(){
        //return new DirectExchange(CONFIRM_EXCHANGE_NAME);

        return ExchangeBuilder
                .directExchange(CONFIRM_EXCHANGE_NAME)
                .durable(true)
                .build();
    }

    //声明队列
    @Bean
    public Queue confirmQueue(){
        return QueueBuilder
                .durable(CONFIRM_QUEUE_NAME)
                .build();
    }

    //绑定 将队列绑定到交换机
    @Bean
    public Binding bindconfirmQueueToConfirmExchange(
            @Qualifier("confirmQueue") Queue confirmQueue,
            @Qualifier("confirmExchange") DirectExchange confirmExchange){

        return BindingBuilder
                .bind(confirmQueue)
                .to(confirmExchange)
                .with(CONFIRM_ROUTING_KEY);
    }
}
